package com.eedi.framework.protocol.controller.sys;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.common.validation.AddGroup;
import com.eedi.framework.common.validation.UpdateGroup;
import com.eedi.framework.protocol.controller.sys.vo.SysProtocolPageReq;
import com.eedi.framework.protocol.controller.sys.vo.SysProtocolSaveReq;
import com.eedi.framework.protocol.controller.sys.vo.SysProtocolResp;
import com.eedi.framework.protocol.controller.sys.vo.SysProtocolUpdateStatusReq;
import com.eedi.framework.protocol.dal.dataobject.ProtocolDO;
import com.eedi.framework.protocol.service.SysProtocolService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;


@Tag(name = "系统界面 - 协议配置")
@RestController
@RequestMapping("/protocol")
@Validated
public class SysProtocolController {

    @Resource
    private SysProtocolService sysProtocolService;

    @PostMapping("/create")
    @Operation(summary = "创建协议配置")
  //  @PreAuthorize("@ss.hasPermission('system:protocol:create')")
    public CommonResult<String> createProtocol(@Validated({AddGroup.class}) @RequestBody SysProtocolSaveReq createReqVO) {
        return success(sysProtocolService.createProtocol(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新协议配置")
 //   @PreAuthorize("@ss.hasPermission('system:protocol:update')")
    public CommonResult<Boolean> updateProtocol(@Validated({UpdateGroup.class}) @RequestBody SysProtocolSaveReq updateReqVO) {
        sysProtocolService.updateProtocol(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除协议配置")
    @Parameter(name = "systemProtocolId", description = "编号", required = true)
 //   @PreAuthorize("@ss.hasPermission('system:protocol:delete')")
    public CommonResult<Boolean> deleteProtocol(String systemProtocolId) {
        sysProtocolService.deleteProtocol(systemProtocolId);
        return success(true);
    }

    @PostMapping("/update-status")
    @Operation(summary = "修改协议配置发布状态")
    //   @PreAuthorize("@ss.hasPermission('system:banner:updateStatus')")
    @PermitAll
    public CommonResult<Boolean> updateProtocolStatus(@Valid @RequestBody SysProtocolUpdateStatusReq reqVO) {
        sysProtocolService.updateProtocolStatus(reqVO.getSystemProtocolId(), reqVO.getPublishStatus());
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "获得协议配置分页")
  //  @PreAuthorize("@ss.hasPermission('system:protocol:query')")
    public CommonResult<PageResult<SysProtocolResp>> getProtocolPage(@RequestBody @Valid SysProtocolPageReq pageReqVO) {
        PageResult<ProtocolDO> pageResult = sysProtocolService.getProtocolPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SysProtocolResp.class));
    }

}
